#include <iostream>
#include <memory>
using namespace std;
class Person
{
public:
    Person()
    {
        std::cout << "无参函数\n";
    }
    ~Person()
    {
        std::cout << "析构函数\n";
    }

    void run()
    {
        cout << "run\n";
    }

private:
    int m_age;
    char *m_name;
};

template <typename T>
class smartPtr
{
private:
    T *ptr;

public:
    smartPtr(T *obj)
    {
        ptr = obj;
    }
    ~smartPtr()
    {
        delete ptr;
    }

public:
    T *operator->()
    {
        return ptr;
    }
};
int main()
{
    // {
    //     Person *per = new Person[3];
    //     delete per;
    // }

#if 0
    /*autoptr不能用于数组*/
    auto_ptr<Person> p1(new Person);
    // p1->run();
    p1->run();
#endif
#if 0
    smartPtr<Person> sp(new Person);
    sp->run();
#endif
    /*引用计数*/
    /*最后一个指向对象的ptr作用域结束之后才析构*/
    {
        shared_ptr<Person> p2;
        shared_ptr<Person> p3;
        {
            shared_ptr<Person> p1(new Person);
            p2 = p1;
            cout << "count=" << p1.use_count() << endl;
        }
        cout << "count=" << p2.use_count() << endl;
        cout << "2222\n";
        p3 = p2;
        cout << "count=" << p3.use_count() << endl;
    }
    cout << "33333\n";
    // p1->run();

    /*是否适用数组*/
    // shared_ptr<Person[]> p2(new Person[3]{});
}